import csv
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from loguru import logger
import sys

# 龙脊梯田 addr = 'https://you.ctrip.com/sight/longsheng1445120/14892.html'
# 象鼻山 addr = 'https://you.ctrip.com/sight/guilin28/107505.html'
# 两江四湖 addr = 'https://you.ctrip.com/sight/guilin28/5925.html'
# 独秀峰景区 addr = 'https://you.ctrip.com/sight/guilin28/107501.html'
# 芦笛岩 addr = 'https://you.ctrip.com/sight/guilin28/2880.html?scene=online'
# 七星景区 addr = 'https://you.ctrip.com/sight/guilin28/2882.html?scene=online'
# 漓江景区 addr = 'https://you.ctrip.com/sight/lingchuan3064/2888.html?scene=online'
# 遇龙河 addr ='https://you.ctrip.com/sight/yangshuo702/137485.html?scene=online'
# 遇龙河景区 addr = 'https://you.ctrip.com/sight/yangshuo702/22081.html?scene=online'
# 叠彩山 addr = 'https://you.ctrip.com/sight/guilin28/2879.html?scene=online'
# 三星游船 addr = 'https://you.ctrip.com/sight/lingchuan3064/1478560.html?scene=online'
# 独秀峰 addr = 'https://you.ctrip.com/sight/guilin28/2886.html?scene=online'
# 世外桃源 addr = 'https://you.ctrip.com/sight/yangshuo702/140484.html?scene=online'
# 桂林千古情 addr = 'https://you.ctrip.com/sight/yangshuo702/4734370.html?scene=online'
# 银子岩 addr = 'https://you.ctrip.com/sight/lipu2020/18147.html?scene=online'
# 十里画廊 addr = 'https://you.ctrip.com/sight/yangshuo702/52551.html?scene=online'
# 兴萍古镇 addr = 'https://you.ctrip.com/sight/yangshuo702/26887.html?scene=online',
# 西街  addr =  'https://you.ctrip.com/sight/yangshuo702/22079.html?scene=online',
# 相公山  addr =  https://you.ctrip.com/sight/yangshuo702/1417461.html?scene=online

addr_list = ['https://you.ctrip.com/sight/guilin28/2879.html?scene=online',
             'https://you.ctrip.com/sight/yangshuo702/137485.html?scene=online',
             'https://you.ctrip.com/sight/longsheng1445120/14892.html',
             'https://you.ctrip.com/sight/yangshuo702/52551.html?scene=online',
             'https://you.ctrip.com/sight/guilin28/5925.html',
             'https://you.ctrip.com/sight/guilin28/2880.html?scene=online',
             'https://you.ctrip.com/sight/guilin28/107505.html',
             'https://you.ctrip.com/sight/yangshuo702/22081.html?scene=online',
             'https://you.ctrip.com/sight/guilin28/107501.html',
             'https://you.ctrip.com/sight/lingchuan3064/1478560.html?scene=online',
             'https://you.ctrip.com/sight/guilin28/2882.html?scene=online',
             'https://you.ctrip.com/sight/guilin28/2886.html?scene=online',
             'https://you.ctrip.com/sight/yangshuo702/140484.html?scene=online',
             'https://you.ctrip.com/sight/lipu2020/18147.html?scene=online',
             'https://you.ctrip.com/sight/yangshuo702/140484.html?scene=online',
             'https://you.ctrip.com/sight/yangshuo702/4734370.html?scene=online',
             ]


# 进入携程评论页面
def enter_page(addr):
    driver = webdriver.Chrome()
    driver.get(addr)
    driver.maximize_window()

    time.sleep(1)

    driver.find_element(By.XPATH, '//p[@class="commentCount"]//span[@class="hover-underline"]').click()
    ###跳转到新页面
    all_handles = driver.window_handles

    time.sleep(2)

    return all_handles, driver


# 开始爬取评论
def comment_crawler(addr):
    handles, driver = enter_page(addr)
    # next_page_ele = driver.find_element(By.XPATH, '//div[@class="myPagination"]//ul[@class="ant-pagination"]//li//span[@class="ant-pagination-item-comment"]')
    with open('data.csv', mode='a', encoding='utf-8-sig', newline='') as file:
        writer = csv.writer(file)
        while True:
            comments = driver.find_elements(By.CLASS_NAME, 'commentDetail')
            score = driver.find_elements(By.CLASS_NAME, 'averageScore')
            for i in range(len(comments)):
                logger.info(comments[i])
                writer.writerow([comments[i].text, score[i].text[0]])
            has_next_page = driver.find_element(By.CLASS_NAME, 'ant-pagination-next').get_dom_attribute("aria-disabled")
            logger.info(has_next_page)
            if has_next_page == 'true':
                return
            page_no = driver.find_element(By.CLASS_NAME, 'ant-pagination-item-active')
            logger.info(page_no.text)
            next_page_ele = driver.find_element(By.XPATH,
                                                '//div[@class="myPagination"]//ul//li[@title="下一页"]//span[@class="ant-pagination-item-comment"]//a')
            driver.execute_script("arguments[0].click();", next_page_ele)
            # next_page_ele.click()
            time.sleep(2)
    driver.close()


if __name__ == '__main__':
    for addr in addr_list:
        comment_crawler(addr)
